import json
import threading
from utils.sententialyzer_client import SententialyzerClient
import time

def checkAttentions(self, opId, positive = -1, negative = -1, total_content = -1):
    op = self.sc.getOpSimple(opId, 'most_positive', max_content_length=4, max_depth=0)
    if positive >= 0:
        self.assertEqual(op['positive'], positive)
    
    if negative >= 0:
        self.assertEqual(op['negative'], negative)
        
    if total_content >= 0:
        self.assertEqual(op['total'], total_content)
        
def checkSubopStats(self, opId, positive = -1, negative = -1, neutral = -1, content = -1):
    pass
#    op = self.sc.getOpSimple(opId, 'most_positive', max_content_length=4, max_depth=0)
#    
#    if positive >= 0:
#        self.assertEqual(op['num_positive_subops'], positive)
#    
#    if negative >= 0:
#        self.assertEqual(op['num_negative_subops'], negative)
#    
#    if neutral >= 0:
#        self.assertEqual(op['num_neutral_subops'], neutral)
#        
#    if content >= 0:
#        self.assertEqual(op['num_content_subops'], content)
#        
        
def check_opinion_tree(self, baseOpId, expected_total_children=-1):
    result = self.sc.getOpExt(baseOpId, {'tree_order': 'depth_first',
                                             'order': 'oldest',
                                             'max_depth': 10,
                                             'max_subopinions': 100,
                                             'first_subopinion': 0})
    print json.dumps(result)
    if expected_total_children >= 0:
        self.assertEqual(expected_total_children, result['total'])
        
    check_opinion_tree_order(self, result, 1)
    
def check_opinion_tree_order(self, op, nested_set_order):
    self.assertEqual(nested_set_order, op['nested_set_left'])
    
    nested_set_order += 1
    
    if op.has_key('sub_opinions'):
        for subop in op['sub_opinions']:
            self.assertEqual(op['id'], subop['parent'])
            nested_set_order = check_opinion_tree_order(self, subop, nested_set_order)
            nested_set_order += 1
    
    self.assertEqual(nested_set_order, op['nested_set_right'])
    return nested_set_order


class ClientThread(threading.Thread):
    def __init__(self, user_id, do_autorize=True):
        threading.Thread.__init__(self)
        self.async_sc = SententialyzerClient()
        
        self.user_id = user_id
        self.execution_time=0
        self.commit_delay=0
        
        if do_autorize:
            self.authorize()
            
    def authorize(self):
        self.async_sc.authorize('commenter_sync_' + str(self.user_id), 'emptypass', 'all:rc')
        
    def operation(self):
        pass
    
    def run(self):
        time_start = time.time()
        self.operation()
        self.execution_time = time.time() - time_start